package com.xc.ksxt.controller;

import com.xc.common.object.LayuiTableResponse;
import com.xc.common.object.PageInfo;
import com.xc.common.object.ResponseEntity;
import com.xc.common.tool.Helper;
import com.xc.db.tables.LibCategory;
import com.xc.ksxt.entity.LibCategoryEntity;
import com.xc.ksxt.service.LibCategoryService;
import com.xc.ksxt.vo.LibCategoryVO;
import com.xc.sys.entity.SysDictEntity;
import com.xc.sys.service.SysDictService;
import com.xc.sys.service.SysUserService;
import com.xc.sys.vo.SysUserVO;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.Record;
import org.jooq.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/libCategory")
public class LibCategoryController {

    @Autowired
    private LibCategoryService libCategoryService;
    private LibCategory LIBCATEGORY = LibCategory.LIB_CATEGORY.as("LIBCATEGORY");
    @CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.GET})
    @RequestMapping({"/getPageList"})
    public LayuiTableResponse<LibCategoryVO> getPageList(HttpServletRequest request) {
        LayuiTableResponse<LibCategoryVO> ltr = new LayuiTableResponse<>();
        Condition cond = LIBCATEGORY.DEL_FLAG.notEqual(-1);
        String name = request.getParameter("name");
        String keywords = request.getParameter("keywords");
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        Integer page = Integer.valueOf(request.getParameter("page"));
        if (StringUtils.isNotBlank(name)) {
            cond = cond.and(LIBCATEGORY.NAME.like("%"+name+"%"));
        }
        if (StringUtils.isNotBlank(keywords)) {
            cond = cond.and(LIBCATEGORY.NAME.like("%"+keywords+"%"));
        }
        Long count = libCategoryService.count(cond);
        ltr.setCount(count);
        PageInfo<LibCategoryEntity> pageInfo = new PageInfo<>(limit, page);
        Result<Record> list = libCategoryService.select(cond, pageInfo,LIBCATEGORY.ORDERNO.asc(),LIBCATEGORY.NAME.asc(),LIBCATEGORY.CREATE_TIME.desc());
        List<LibCategoryVO> libCategoryEntityList = list.into(LibCategoryVO.class);
        for (LibCategoryVO libCategoryEntity : libCategoryEntityList){
            SysUserVO user = SysUserService.getFrameCaches(libCategoryEntity.getCreateUserId());
            libCategoryEntity.setCreateUserName(user==null?"":user.getName());
            SysDictEntity dict = SysDictService.getFromCaches("rvalid-state," + libCategoryEntity.getState());
            libCategoryEntity.setStateTxt(dict==null?"":dict.getName());
        }
        ltr.setData(libCategoryEntityList);
        return ltr;
    }
    @CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.GET})
    @RequestMapping({"/getById"})
    public LibCategoryEntity getById(Long id) {
        LibCategoryEntity entity = libCategoryService.getEntityById(id);
        return entity;
    }
    @CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.POST})
    @RequestMapping({"/save"})
    public ResponseEntity<LibCategoryEntity> save(@RequestBody HashMap<String,String> params, HttpServletRequest request) {
        ResponseEntity<LibCategoryEntity> res = new ResponseEntity<>();
        LibCategoryEntity libCategoryEntity = Helper.map2Bean(new LibCategoryEntity(), params);
        libCategoryService.save(libCategoryEntity);
        return res;
    }
    @CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.POST})
    @RequestMapping({"/delete"})
    public LayuiTableResponse<LibCategoryEntity> delete(@RequestBody Map<String,String> params) {
        String ids = params.get("ids");
        Long[] idarr = Helper.str2longArr(ids);
        LayuiTableResponse<LibCategoryEntity> ltr = new LayuiTableResponse<>();
        for (Long id : idarr) {
            LibCategoryEntity m = libCategoryService.getEntityById(id);
            m.setDelFlag(-1);
            libCategoryService.update(m);
        }
        return ltr;
    }

}
